package parser

import (
	"cloudDevice2/sampleapi/protobuf"
	"fmt"
	"log"

	"cloudDevice2/sampleapi/utils"

	"github.com/golang/protobuf/proto"
)

func ProceedAlarmV2(pbData []byte) {
	alarmConfirm := &protobuf.AlarmInfokConfirm{}
	err := proto.Unmarshal(pbData, alarmConfirm)
	if err != nil {
		log.Println(fmt.Sprintf("parse alarm v2 error: %s", err.Error()))
		return
	}
	pbAlarm1 := alarmConfirm.GetAlarm()
	if pbAlarm1 != nil {
		hr_alarm_list := pbAlarm1.GetAlarmHr()
		if len(hr_alarm_list) > 0 {
			for _, hr_alarm := range hr_alarm_list {
				seconds := int64(hr_alarm.GetTimeStamp().GetDateTime().GetSeconds())
				alarm_time_str := utils.ParsePbDateTime(seconds)
				hr := hr_alarm.GetHr()
				log.Println(fmt.Sprintf("----%s hr alarm, hr:%d", alarm_time_str, hr))
			}
		}

		spo2_alarm_list := pbAlarm1.GetAlarmSpo2()
		if len(spo2_alarm_list) > 0 {
			for _, spo2_alarm := range spo2_alarm_list {
				seconds := int64(spo2_alarm.GetTimeStamp().GetDateTime().GetSeconds())
				alarm_time_str := utils.ParsePbDateTime(seconds)
				boxy := spo2_alarm.GetSpo2()
				log.Println(fmt.Sprintf("----%s spo2 alarm, boxy:%d", alarm_time_str, boxy))
			}
		}

		thrombus_alarm_list := pbAlarm1.GetAlarm_Thrombus()
		if len(thrombus_alarm_list) > 0 {
			for _, thrombus_alarm := range thrombus_alarm_list {
				seconds := int64(thrombus_alarm.GetTimeStamp().GetDateTime().GetSeconds())
				alarm_time_str := utils.ParsePbDateTime(seconds)
				log.Println(fmt.Sprintf("----%s thrombus alarm", alarm_time_str))
			}
		}

		fall_alarm_list := pbAlarm1.GetAlarmFall()
		if len(fall_alarm_list) > 0 {
			for _, fall_alarm := range fall_alarm_list {
				seconds := int64(fall_alarm.GetTimeStamp().GetDateTime().GetSeconds())
				alarm_time_str := utils.ParsePbDateTime(seconds)
				log.Println(fmt.Sprintf("----%s fall alarm", alarm_time_str))
			}
		}

		tmpr_alarm_list := pbAlarm1.GetAlarm_Temperature()
		if len(tmpr_alarm_list) > 0 {
			for _, tmpr_alarm := range tmpr_alarm_list {
				seconds := int64(tmpr_alarm.GetTimeStamp().GetDateTime().GetSeconds())
				alarm_time_str := utils.ParsePbDateTime(seconds)
				tmpr := tmpr_alarm.GetTemperature()
				log.Println(fmt.Sprintf("----%s temperature alarm, temperature:%d",
					alarm_time_str, tmpr))
			}
		}

		bp_alarm_list := pbAlarm1.GetAlarm_Bp()
		if len(bp_alarm_list) > 0 {
			for _, bp_alarm := range bp_alarm_list {
				seconds := int64(bp_alarm.GetTimeStamp().GetDateTime().GetSeconds())
				alarm_time_str := utils.ParsePbDateTime(seconds)
				sbp := bp_alarm.GetSbp()
				dbp := bp_alarm.GetDbp()
				log.Println(fmt.Sprintf("----%s blood pressure alarm, sbp:%d,dbp:%d",
					alarm_time_str, sbp, dbp))
			}
		}

		//sos alarm, will send an alarm without location first, before do locate/call
		sos_time := pbAlarm1.GetSOS_NotificationTime()
		if sos_time != nil {
			seconds := int64(sos_time.GetDateTime().GetSeconds())
			alarm_time_str := utils.ParsePbDateTime(seconds)
			log.Println(fmt.Sprintf("----sos alarm time %s", alarm_time_str))
		}
	}

	pbAlarm2 := alarmConfirm.GetAlarminfo()
	if pbAlarm2 != nil {
		seconds := int64(pbAlarm2.GetTimeStamp().GetDateTime().GetSeconds())
		alarm_time_str := utils.ParsePbDateTime(seconds)

		if pbAlarm2.LowpowerPercentage != nil {
			power := pbAlarm2.GetLowpowerPercentage()
			log.Println(fmt.Sprintf("----%s low power alarm, battery:%d",
				alarm_time_str, power))
		}

		if pbAlarm2.PoweroffPercentage != nil {
			power := pbAlarm2.GetPoweroffPercentage()
			log.Println(fmt.Sprintf("----%s power off alarm, battery:%d",
				alarm_time_str, power))
		}

		if pbAlarm2.Wearstate != nil {
			log.Println(fmt.Sprintf("----%s not wear alarm",
				alarm_time_str))
		}

		if pbAlarm2.InterceptNumber != nil {
			number := pbAlarm2.GetInterceptNumber()
			log.Println(fmt.Sprintf("----%s phone intercept alarm, phone number:%s",
				alarm_time_str, number))
		}
	}
}
